package com.etc.controller;

import com.etc.entity.Books;
import com.etc.entity.Bookstype;
import com.etc.service.BooksService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "图书浏览相关接口")
@RequestMapping("/frontbooks")
@CrossOrigin
public class FrontBookController {
    @Autowired
    BooksService service;

    @RequestMapping("/getAll")
    @ApiOperation("全部图书接口")
    public List<Books> getBooks(){
        List<Books> list = service.getAllBooks();
        return list;
    }

    @RequestMapping("/getById")
    @ApiOperation("图书详情接口")
    public Books getBooks(@RequestParam int bId){
        return service.getById(bId);
    }

    @RequestMapping("/getByType")
    @ApiOperation("分类查询接口")
    public PageInfo<Books> getBookByType(@RequestParam(value = "btName",defaultValue = "")String btName, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "2")int size){
        List<Books> books = service.getByType(btName,pageNum,size);
        PageInfo<Books> pageInfo=new PageInfo<Books>(books);
        return pageInfo;
    }

    @RequestMapping("/getByLike")
    @ApiOperation("模糊查询接口")
    public PageInfo<Books> getBookByLike(@RequestParam(value = "bName",defaultValue = "")String bName, @RequestParam(value = "bAuthor",defaultValue = "")String bAuthor, @RequestParam(value = "btName",defaultValue = "")String btName,@RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "2")int size){
        List<Books> books = service.getByLike(bName,bAuthor,btName,pageNum,size);
        PageInfo<Books> pageInfo=new PageInfo<Books>(books);
        return pageInfo;
    }
    @RequestMapping("/getTypes")
    @ApiOperation("图书类型接口")
    public List<Bookstype> getTypes() {
        List<Bookstype> list = service.getAllType();
        return list;
    }
}
